package cn.cctvcc.system.controller;

import cn.cctvcc.core.domain.R;
import cn.cctvcc.core.enums.common.CommonEnum;
import cn.cctvcc.core.utils.SecurityUtils;
import cn.cctvcc.core.utils.StringUtil;
import cn.cctvcc.core.web.controller.BaseController;
import cn.cctvcc.core.web.page.TableDataInfo;
import cn.cctvcc.system.api.domain.LoginUser;
import cn.cctvcc.system.api.domain.SysUser;
import cn.cctvcc.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @description: 用户信息控制层
 * @author: Jiang
 * @create: 2021-09-22 14:56
 */
@RestController
@RequestMapping("/sysUser")
public class SysUserController extends BaseController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 列表查询
     * @param sysUser
     * @return
     */
    @GetMapping(value = "/list")
    public TableDataInfo selectPage(SysUser sysUser) {
        return getDataTable(sysUserService.selectPage(sysUser));
    }

    /**
     * 新增用户信息
     */
    @PostMapping(value = "/add")
    public R<SysUser> add(@RequestBody SysUser sysUser, @RequestParam("isRegister") Boolean isRegister) {
        if (Objects.equals(CommonEnum.NO.getCode(), sysUserService.checkUserNameUnique(sysUser)))
            return R.fail("用户名：" + sysUser.getUserName() + " ,已存在");
        return R.ok(sysUserService.add(sysUser, isRegister));
    }

    /**
     * 编辑用户信息
     */
    @PutMapping(value = "/edit")
    public R<Integer> edit(@RequestBody SysUser sysUser) {
        SysUser user = sysUserService.selectUserByUserId(sysUser.getUserId());
        if (Objects.isNull(user))
            return R.fail("用户不存在");
        if (Objects.equals(CommonEnum.NO.getCode(), sysUserService.checkUserNameUnique(sysUser)))
            return R.fail("用户名：" + sysUser.getUserName() + " ,已存在");
        if (Objects.equals(CommonEnum.NO.getCode(), sysUserService.checkEmailUnique(sysUser)))
            return R.fail("邮箱：" + sysUser.getEmail() + " ,已绑定用户");
        user.setNickName(sysUser.getNickName());
        user.setEmail(sysUser.getEmail());
        user.setPhonenumber(sysUser.getPhonenumber());
        user.setSex(sysUser.getSex());
        user.setAvatar(sysUser.getAvatar());
        user.setUpdateBy(SecurityUtils.getUsername());
        return R.ok(sysUserService.update(user));
    }

    /**
     * 根据用户名称获取用户
     */
    @GetMapping(value = "/getUserByUserName/{userName}")
    public R<LoginUser> getUserByUserName(@PathVariable("userName") String userName) {
        if (Objects.isNull(userName))
            return R.fail("用户名必需填写");
        SysUser sysUser = sysUserService.selectUserByUserName(userName);
        if (Objects.isNull(sysUser))
            return R.fail("用户未注册");
        LoginUser loginUser = new LoginUser();
        loginUser.setSysUser(sysUser);
        return R.ok(loginUser);
    }

    /**
     * 获取当前登录用户信息
     */
    @GetMapping(value = "/getLoginInfo")
    public R<SysUser> getLoginInfo() {
        Long userId = SecurityUtils.getUserId();
        if (Objects.isNull(userId))
            return R.fail("非法操作，尚未登录");
        SysUser sysUser = sysUserService.selectUserByUserId(userId);
        sysUser.setPassword(null);
        return R.ok(sysUser);
    }

    /**
     * 检查用户是否唯一
     */
    @GetMapping(value = "/checkUserNameUnique/{userName}")
    public R<String> checkUserNameUnique(@PathVariable("userName") String userName) {
        if (Objects.isNull(userName))
            return R.fail("用户名必需填写");
        SysUser sysUser = new SysUser();
        sysUser.setUserName(userName);
        return R.ok(sysUserService.checkUserNameUnique(sysUser));
    }

    /**
     * 重置密码
     */
    @PutMapping(value = "/resetpass")
    public R<Integer> resetpass(@RequestBody SysUser user) {
        if (Objects.isNull(user.getUserName()))
            return R.fail("用户名必需填写");
        SysUser sysUser = sysUserService.selectUserByUserName(user.getUserName());// 获取用户
        if (Objects.isNull(sysUser))
            return R.fail("用户名：" + user.getUserName() + " ,不存在");
        sysUser.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        sysUser.setPwdUpdateDate(LocalDateTime.now());
        sysUser.setUpdateBy(SecurityUtils.getUsername());
        return R.ok(sysUserService.update(sysUser));
    }

    /**
     * 修改当前登录用户密码
     */
    @PutMapping(value = "/updatePassword")
    public R<Integer> updatePassword(@RequestParam("oldPassword") String oldPassword,
                                     @RequestParam("password") String password) {
        if (StringUtil.isEmpty(oldPassword) || StringUtil.isEmpty(password))
            return R.fail("用户密码必需填写");
        SysUser sysUser = sysUserService.selectUserByUserId(SecurityUtils.getUserId());
        if (Objects.isNull(sysUser))
            return R.fail("非法操作，尚未登录");
        if (!SecurityUtils.matchesPassword(oldPassword, sysUser.getPassword()))
            return R.fail("原密码不正确");
        sysUser.setPassword(SecurityUtils.encryptPassword(password));
        sysUser.setPwdUpdateDate(LocalDateTime.now());
        sysUser.setUpdateBy(SecurityUtils.getUsername());
        return R.ok(sysUserService.update(sysUser));
    }

}
